﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using StartStop.Helpers; 

namespace websitev2.Controllers
{
    public class DeveloperController : Controller
    {
        //
        // GET: /Developers/

        public ActionResult Index()
        {
            // First thing to do is check to see if they are registered, because if they are, we can push them to the control panel. 
            if (Request.IsAuthenticated)
            {
                if (Session["oLogged"] == null)
                {

                    // We need to run some checks to make sure the session object is populates
                    Guid UserGUID = Guid.Parse(Membership.GetUser().ProviderUserKey.ToString());

                    // next we load the loggedinuser information into the session as we'll be needing it
                    Models.User.LoggedIn oLogged = Core.UserCore.LoadLoggedInUserDetails(UserGUID);
                    // Populate the user session.
                    Session["oLogged"] = oLogged;
                    Session["oUsername"] = oLogged.UserName;
                }

                Models.User.LoggedIn oUser =  (Models.User.LoggedIn)Session["oLogged"]; 
                StartStop.ServiceResources.MessageResponse oResponse = Devs.IsDevRegistered(oUser.UserID); 

                if (oResponse.Success)
                {
                    Session["DeveloperID"] = oResponse.ReturnID; 
                    Response.Redirect("/Developer/ControlPanel/"); 
                }
                 
            }
            return View();
        }

        [HttpPost]
        public ActionResult Index(websitev2.Models.Developers.Register oReg)
        {
            // Just double check that it is indeed authenticated. 
            if ((ModelState.IsValid)&&(Request.IsAuthenticated))
            {
                if (Session["oLogged"] == null)
                {

                    // We need to run some checks to make sure the session object is populates
                    Guid UserGUID = Guid.Parse(Membership.GetUser().ProviderUserKey.ToString());

                    // next we load the loggedinuser information into the session as we'll be needing it
                    Models.User.LoggedIn oLogged = Core.UserCore.LoadLoggedInUserDetails(UserGUID);
                    // Populate the user session.
                    Session["oLogged"] = oLogged;
                    Session["oUsername"] = oLogged.UserName;
                }

                try
                {
                    // Here we need to add the developer record. 
                    StartStop.ServiceResources.Developer oDev = new StartStop.ServiceResources.Developer();
                    oDev.Approved = false;
                    oDev.Description = oReg.Description;
                    oDev.SiteURL = oReg.SiteURL;
                    oDev.Approved = true; 

                    Models.User.LoggedIn oLogged = new Models.User.LoggedIn();
                    // Populate the user session.
                    oLogged = (Models.User.LoggedIn)Session["oLogged"];

                    oDev.UserID = oLogged.UserID;

                    StartStop.ServiceResources.MessageResponse oResponse =   StartStop.Helpers.Devs.UpdateDeveloper(oDev);

                    if (oResponse.Success)
                    {
                        // Add the developer to the developer role. 
                        Roles.AddUserToRole(oLogged.UserName, "Developer");

                        // Redirect to the developers initial PAGE
                        Response.Redirect("/Developer/ControlPanel/"); 
                    }

                }
                catch (Exception ex)
                
                {

                    StartStop.ServiceResources.ErrorEvent oError = new StartStop.ServiceResources.ErrorEvent();
                    oError.CreatedOn = DateTime.Now;
                    oError.ErrorDetail = ex.Message.ToString();
                    oError.ErrorLevel = StartStop.ServiceResources.ErrorEvent.ErrorLevelType.Critical;
                    oError.ErrorMessage = "Cannot write developer record";

                    StartStop.Helpers.EventLog.LogError(oError); 

                }
            }
            return View(oReg);
        }



    
    }
}
